// pages/news/newsDetails/newsDetails.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:'',
        info: {},
        info2: [],
        msg: '',
        title: '',
        time: '',
        bottom: 0,
        comment: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.id) {
            this.setData({
                id:options.id
            });
            this.getContent();
            this.getInfo();
        };
        if (options.comment === 'comment') {
          this.setData({
            comment: true
          });
        };
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // 分享
    onShareAppMessage(res) {
        return {
            title: '广州大学校友会校友企业家专委会',
            path: '/pages/index/index'
        }
    },
    getContent: function () {
      let _this = this;
      app.util.ajax({
          url: 'platform/detail',
          data: {
            id: _this.data.id,
            user_id: app.globalData.userInfo.user_id
          },
          success: function(res) {
            let data = res;
            data.content = data.content.replace(/<img/g, '<img style="width:100%;"');
            _this.setData({
                info: data
            });
          }
      })
    },
    // 是否点赞
    isPraise:function(e){
        let state = e.currentTarget.dataset.state ? 0 : 1,
            _this = this;
        app.util.ajax({
            url:'platform/zambia',
            data:{
                id:_this.data.id,
                user_id:app.globalData.userInfo.user_id,
                state:state
            },
            success:function(data){
                _this.getInfo();
                _this.getContent();
            }
        })
    },
  getInfo: function () {
    let _this = this;
    app.util.ajax({
      url: "Comment/index",
      data: {
        id: _this.data.id,
        type: 1
      },
      success: function (res) {
        let array = [];
        for (var i in res.data) {
          if (typeof res.data[i] === 'object' && res.data[i] !== null) {
            array.push(res.data[i]);
          };
        };
        _this.setData({
          info2: array,
          title: res.data.title,
          time: res.data.add_time
        })
      }
    })
  },
  getMsg: function (e) {
    this.setData({
      msg: e.detail.value
    })
  },
  send: function () {
    let _this = this;
    if (_this.data.msg === '') {
      wx.showToast({
        title: '请输入评论',
        icon: 'none',
        duration: 2000
      })
    } else {
      app.util.ajax({
        url: "Comment/add",
        method: 'post',
        data: {
          info: {
            id: _this.data.id,
            user_id: app.globalData.userInfo.user_id,
            content: _this.data.msg,
            type: 1
          }
        },
        success: function (res) {
          if (res.state === 1) {
            wx.showToast({
              title: res.info,
              icon: 'success',
              duration: 2000
            })
            _this.getInfo();
          } else {
            wx.showToast({
              title: res.info,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    };
  },
  foucus: function (e) {
    var _this = this;
    _this.setData({
      bottom: e.detail.height
    })
  },
  blur: function (e) {
    var _this = this;
    _this.setData({
      bottom: 0
    })
  }
})